<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Installation script for
 *
 * @author No-CMS Module Generator
 */
class scrapm extends CMS_Priv_Strict_Controller {

    private $_academic_types = array('1'=>'Regular','2'=>'Special education','3'=>'Vocational','4'=>'Alternative/other');
    private $_operational_status = array('1' => 'Currently operational',
                                         '2' => 'New School',
                                         '3' => 'School operational but not reported last year',
                                         '4' => 'School has changed agency',
                                         '5' => 'School reopened');
    
    protected function do_override_url_map($URL_MAP){
        $module_path = $this->cms_module_path();
        $navigation_name = $this->cms_complete_navigation_name('index');
        $URL_MAP[$module_path.'/'.$module_path] = $navigation_name;
        $URL_MAP[$module_path] = $navigation_name;
        return $URL_MAP;
    }

    public function index(){
    	$data['content'] = $this->cms_submenu_screen($this->cms_complete_navigation_name('index'));
        $this->view($this->cms_module_path().'/scrapm_index', $data,
            $this->cms_complete_navigation_name('index'));
    }
    
    public function management()
    {
        
        $this->cms_guard_page('scrapm_management');
        
        $data = array(
            "scrapm_submenu_screen" => $this->cms_submenu_screen('scrapm_management')
        );
        $this->view('scrapm/management', $data, 'scrapm_management');
    }
    
    public function user()
    {
        $this->cms_guard_page('scrapm_user_management');
        $crud = new grocery_CRUD();
        $crud->unset_jquery();

        $crud->set_table(cms_table_name('scrapm_users'));
        $crud->set_subject('User');

        $crud->required_fields('academic_name');

        $crud->columns('firstname','lastname','email_address','gender','dob','facebook_idn','date_added','date_updated','is_active');
        $crud->edit_fields('firstname','lastname','email_address','gender','dob','facebook_idn','date_added','date_updated','is_active');
        $crud->add_fields('firstname','lastname','email_address','gender','dob','facebook_idn','date_added','date_updated','is_active');
        
        $crud->callback_column('gender',array($this,'_callback_gender'));
        $crud->callback_column('is_active',array($this,'_callback_column_navigation_active'));

        $crud->display_as('is_active', 'Status')->display_as('date_added', 'Added On')->display_as('date_updated', 'Updated On');
        $crud->field_type('gender', 'enum',array('M'=>'Male','F'=>'Female'));
        $crud->field_type('is_active', 'true_false');
        $output = $crud->render();

        $this->view('scrapm/user', $output, 'scrapm_user_management');    
    }
    
    public function _callback_gender($primary_key,$row)
    {
        if ( $row->gender == 'M' )
        {
            return 'Male';
        } else {
            return 'Female';
        }
    }
    
    public function _callback_column_navigation_active($value, $row)
    {
        //$target = site_url($this->cms_module_path() . '/toggle_navigation_active/' . $row->id);
        if ($value == 0) {
            return '<span class="navigation_active">Inactive</span>';
        } else {
            return '<span class="navigation_active">Active</span>';
        }
    }
    
    public function _callback_column_navigation_academic_active($value, $row)
    {
        $target = site_url($this->cms_module_path() . '/toggle_navigation_active/' . $row->academic_id);
        if ($value == 0) {
            return '<span target="' . $target . '" class="navigation_active">Inactive</span>';
        } else {
            return '<span target="' . $target . '" class="navigation_active">Active</span>';
        }
    }
    
    public function _callback_column_school_type($value, $row)
    {
        return '<span class="navigation_active">'.ucfirst(strtolower($value)).'</span>';
    }
    
    public function _callback_column_academic_type($value, $row)
    {
        return '<span class="navigation_active">'.ucfirst(strtolower($value)).'</span>';
    }
    
    public function academic()
    {
        $this->cms_guard_page('scrapm_academic_management');
        $crud = new grocery_CRUD();
        $crud->unset_jquery();

        $crud->set_table(cms_table_name('scrapm_academic_years'));
        $crud->set_subject('Academic');

        $crud->required_fields('academic_name');

        $crud->columns('academic_name','academic_is_current');
        $crud->edit_fields('academic_name', 'academic_is_current');
        $crud->add_fields('academic_name','academic_is_current');
        $crud->field_type('academic_is_current', 'true_false');
        
        $crud->callback_column('academic_is_current',array($this,'_callback_column_navigation_active'));
    
        $crud->display_as('academic_name', 'Academic Year')->display_as('academic_is_current', 'Status');

        $output = $crud->render();

        $this->view('scrapm/academic', $output, 'scrapm_academic_management');    
    }
    
    public function school()
    {
        $this->cms_guard_page('scrapm_school_management');
        $crud = new grocery_CRUD();
        $crud->unset_jquery();

        $crud->set_table(cms_table_name('scrapm_schools'));
        $crud->set_subject('School');
        
        $crud->required_fields('school_name','school_city','school_state');

        $crud->columns('school_name','school_district','school_county','school_state','school_academic_type','school_academic_level','school_operational_status','school_type','school_is_active');
        
        $crud->set_relation('school_state',cms_table_name('scrapm_states'),'{state_name} ({state_code})');
        ////$crud->set_relation('school_academic_type',cms_table_name('scrapm_academic_types'),'academic_name');
        //$crud->set_relation('school_operational_status',cms_table_name('scrapm_school_operational_statuses'),'operational_status_name');
        
        $crud->edit_fields('school_name','school_address','school_city','school_district','school_county','school_state','school_zipcode','school_geocode_lat','school_geocode_long','school_phone','school_academic_type','school_academic_level','school_operational_status','school_type','school_gender_type','school_religious_affiliation','school_lowest_grade','school_highest_grade','school_has_library','school_is_active');
        $crud->add_fields('school_name','school_address','school_city','school_district','school_county','school_state','school_zipcode','school_geocode_lat','school_geocode_long','school_phone','school_academic_type','school_academic_level','school_operational_status','school_type','school_gender_type','school_religious_affiliation','school_lowest_grade','school_highest_grade','school_has_library','school_is_active');
        
        $crud->field_type('school_has_library', 'enum',array('1'=>'Yes','0'=>'No'));
        $crud->field_type('school_is_active', 'true_false');
        $crud->field_type('school_academic_type', 'dropdown',$this->_academic_types);
        $crud->field_type('school_operational_status', 'dropdown',$this->_operational_status);
        
        $crud->unset_texteditor('school_address','full_text');
        $crud->display_as('school_name', 'Name')->display_as('school_address', 'Address')->display_as('school_city', 'City');
        $crud->display_as('school_state', 'State');
        
        $crud->display_as('school_district', 'District');
        $crud->display_as('school_county', 'County');
        $crud->display_as('school_academic_type', 'Academic Type');
        $crud->display_as('school_academic_level', 'Academic Level');
        $crud->display_as('school_operational_status', 'Opt. Status');
        
        $crud->callback_column('school_is_active',array($this,'_callback_column_navigation_active'));
        $crud->callback_column('school_type',array($this,'_callback_column_school_type'));
        
        $crud->display_as('school_type', 'Type');
        $crud->display_as('school_is_active', 'Status');
        
        //$crud->callback_after_insert(array($this, 'add_default_school_pages'));
        //$crud->callback_before_delete(array($this, 'delete_default_school_pages'));
    
        $output = $crud->render();

        $this->view('scrapm/school', $output, 'scrapm_school_management');    
    }
    
    public function add_default_school_pages($post_array,$primary_key) {
        
        $ins = "INSERT INTO ".cms_table_name('scrapm_pages')." SELECT null,page_name,page_desc,(SELECT academic_id FROM yb_scrapm_academic_years where academic_is_current = 1),'".$primary_key."','active',null,now() FROM ".cms_table_name('scrapm_default_pages')." where page_status = 'active'";
        $this->db->query($ins);
        log_message('ERROR',$this->db->last_query());
    }
    
    public function delete_default_school_pages($primary_key) {
        
        $del = "DELETE FROM ".cms_table_name('scrapm_pages')." WHERE page_school_id = ".$primary_key;
        $this->db->query($del);
        log_message('ERROR',$this->db->last_query());
    }
    
    // done
    public function page()
    {
        $this->cms_guard_page('scrapm_page_management');
        $crud = new grocery_CRUD();
        $crud->unset_jquery();

        $crud->set_table(cms_table_name('scrapm_pages'));
        $crud->set_subject('Page');

        $crud->required_fields('academic_name');

        $crud->columns('page_name','page_academic_year','page_school_id','page_status','page_created_by','page_created_datetime');
        $crud->set_relation('page_academic_year',cms_table_name('scrapm_academic_years'),'academic_name');
        $crud->set_relation('page_school_id',cms_table_name('scrapm_schools'),'school_name');
        $crud->set_relation('page_created_by',cms_table_name('scrapm_users'),'{firstname} {lastname}');
            
        $crud->edit_fields('page_name','page_academic_year','page_school_id','page_status','page_created_by','page_created_datetime');
        $crud->add_fields('page_name','page_academic_year','page_school_id','page_status','page_created_by','page_created_datetime');
        
        $crud->display_as('page_name', 'Page Name')->display_as('page_academic_year', 'Academic Year');
        $crud->display_as('page_school_id', 'School');
        $crud->display_as('page_status', 'Status');
        $crud->display_as('page_created_by', 'Created By');
        $crud->display_as('page_created_datetime', 'Created On');
        
        $output = $crud->render();

        $this->view('scrapm/academic', $output, 'scrapm_page_management');    
    }
    
    public function comments()
    {
        $this->cms_guard_page('scrapm_comment_management');
        $crud = new grocery_CRUD();
        $crud->unset_jquery();

        $crud->set_table(cms_table_name('scrapm_comments'));
        $crud->set_subject('Comment');

        $crud->required_fields('comment_picture_id','comment_text','comment_created_by','comment_status');

        $crud->columns('comment_picture_id','comment_text','comment_created_by','comment_status');
        
        $crud->set_relation('comment_picture_id',cms_table_name('scrapm_pictures'),'picture_thumbnail');
        $crud->set_relation('comment_created_by',cms_table_name('scrapm_users'),'{firstname} {lastname}');
            
        $crud->edit_fields('comment_picture_id','comment_text','comment_created_by','comment_status');
        
        $crud->display_as('comment_picture_id', 'Picture');
        $crud->display_as('comment_text', 'Comment');
        $crud->display_as('comment_status', 'Status');
        $crud->display_as('comment_created_by', 'Created By');
        $crud->display_as('comment_created_datetime', 'Created On');
        $crud->unset_add();
        $crud->unset_delete();
        $crud->unset_texteditor('comment_text','full_text');
        
        $output = $crud->render();

        $this->view('scrapm/comments', $output, 'scrapm_comment_management');    
        
    }
    
    public function likes()
    {
        $this->cms_guard_page('scrapm_likes_management');
        $crud = new grocery_CRUD();
        $crud->unset_jquery();

        $crud->set_table(cms_table_name('scrapm_picture_likes'));
        $crud->set_subject('Like');

        $crud->required_fields('like_picture_id','like_type','like_is_active','like_created_by','like_created_datetime','like_updated_datetime');

        $crud->columns('like_picture_id','like_type','like_is_active','like_created_by','like_created_datetime','like_updated_datetime');
        
        $crud->set_relation('like_picture_id',cms_table_name('scrapm_pictures'),'picture_thumbnail');
        $crud->set_relation('like_created_by',cms_table_name('scrapm_users'),'{firstname} {lastname}');
            
        $crud->edit_fields('like_picture_id','like_type','like_is_active','like_created_by','like_created_datetime','like_updated_datetime');
        
        $crud->display_as('like_picture_id', 'Picture');
        $crud->display_as('like_type', 'Like/Dislike');
        $crud->display_as('like_is_active', 'Status');
        $crud->display_as('like_created_by', 'Created By');
        $crud->display_as('like_created_datetime', 'Created On');
        $crud->display_as('like_updated_datetime', 'Updated On');
        
        $crud->unset_add();
        $crud->unset_delete();
        
        $output = $crud->render();

        $this->view('scrapm/likes', $output, 'scrapm_likes_management');    
        
    }
    
    public function spam()
    {
        $this->cms_guard_page('scrapm_spam_management');
        $crud = new grocery_CRUD();
        $crud->unset_jquery();

        $crud->set_table(cms_table_name('scrapm_spam'));
        $crud->set_subject('Spam');
        
        $crud->required_fields('spam_type','spam_type_id','spam_reason','spam_reported_by','spam_report_datetime');
        $crud->columns('spam_type','spam_type_id','spam_reason','spam_reported_by','spam_report_datetime');
        $crud->set_relation('spam_reported_by',cms_table_name('scrapm_users'),'{firstname} {lastname}');
            
        $crud->edit_fields('spam_type','spam_type_id','spam_reason','spam_reported_by','spam_report_datetime');
        
        $crud->display_as('spam_type_id', 'Identifier');
        $crud->display_as('spam_type', 'Type');
        $crud->display_as('spam_reason', 'Reason');
        $crud->display_as('spam_reported_by', 'Reported By');
        $crud->display_as('spam_report_datetime', 'Reported On');
        
        $crud->unset_add();
        $crud->unset_delete();
        $crud->unset_texteditor('spam_reason','full_text');
        
        $output = $crud->render();

        $this->view('scrapm/spams', $output, 'scrapm_spam_management');    
        
    }
    
    public function pictures()
    {
        $this->cms_guard_page('scrapm_picture_management');
        $crud = new grocery_CRUD();
        $crud->unset_jquery();

        $crud->set_table(cms_table_name('scrapm_pictures'));
        $crud->set_subject('Picture');
        
        $crud->required_fields('picture_page_id','picture_caption','picture_story','picture_thumbnail','picture_fullimage','picture_status','picture_created_by','picture_created_datetime');
        $crud->columns('picture_page_id','picture_caption','picture_thumbnail','picture_status','picture_created_by','picture_created_datetime');
        $crud->set_relation('picture_created_by',cms_table_name('scrapm_users'),'{firstname} {lastname}');
        $crud->set_relation('picture_page_id',cms_table_name('scrapm_pages'),'page_name');
            
        $crud->edit_fields('picture_page_id','picture_caption','picture_story','picture_thumbnail','picture_fullimage','picture_status','picture_created_by','picture_created_datetime');
        
        //'picture_story',picture_fullimage
        $crud->display_as('picture_page_id', 'Page');
        $crud->display_as('picture_caption', 'Caption');
        $crud->display_as('picture_story', 'Story');
        $crud->display_as('picture_status', 'Status');
        $crud->display_as('picture_thumbnail', 'Thumbnail');
        $crud->display_as('picture_fullimage', 'fullimage');
        $crud->display_as('picture_created_by', 'Created By');
        $crud->display_as('picture_created_datetime', 'Created On');
        
        $crud->unset_add();
        $crud->unset_delete();
        $crud->unset_texteditor('picture_story','full_text');
        $crud->unset_texteditor('picture_thumbnail','full_text');
        $crud->unset_texteditor('picture_fullimage','full_text');
        $crud->unset_texteditor('picture_caption','full_text');
        
        $output = $crud->render();

        $this->view('scrapm/spams', $output, 'scrapm_picture_management');    
        
    }
    
    public function defaultPage()
    {
        $this->cms_guard_page('scrapm_defaultpage_management');
        $crud = new grocery_CRUD();
        $crud->unset_jquery();

        $crud->set_table(cms_table_name('scrapm_default_pages'));
        $crud->set_subject('Default Pages');
        
        $crud->required_fields('page_name','page_desc','page_status','page_created_on','page_updated_on');
        $crud->columns('page_name','page_status','page_created_on','page_updated_on');
        
        $crud->add_fields('page_name','page_desc','page_status','page_created_on');    
        $crud->edit_fields('page_name','page_desc','page_status','page_updated_on');
        
        $crud->display_as('page_name', 'Page Name');
        $crud->display_as('page_status', 'Status');
        $crud->display_as('page_created_on', 'Created On');
        $crud->display_as('page_updated_on', 'Updated On');
        $crud->unset_texteditor('page_desc','full_text');
        $output = $crud->render();

        $this->view('scrapm/spams', $output, 'scrapm_defaultpage_management');    
        
    }
    
    public function blacklistedWords()
    {
        $this->config->load('restapi/scrapm');
        $scrapm_config = $this->config->item('scrapm');
        $emoji_path = $scrapm_config['emoji_path'];
        
        $this->cms_guard_page('scrapm_blacklisted_word_management');
        $crud = new grocery_CRUD();
        $crud->unset_jquery();

        $crud->set_table(cms_table_name('scrapm_shit_highschooler'));
        $crud->set_subject('Highschooler Term');
        
        $crud->required_fields('term_word','term_definition','term_type');
        $crud->columns('term_word','term_definition','term_example','term_type');
        
        $crud->add_fields('term_word','term_definition','term_example','term_type','term_image');    
        $crud->edit_fields('term_word','term_definition','term_example','term_type','term_image');
        
        $crud->display_as('term_word', 'Word');
        $crud->display_as('term_example', 'Example');
        $crud->display_as('term_type', 'Type');
        $crud->unset_texteditor('term_definition','full_text');
        $crud->unset_texteditor('term_example','full_text');
        log_message('ERROR',$emoji_path);
        $crud->set_field_upload('term_image',$emoji_path);
        $output = $crud->render();

        $this->view('scrapm/blacklistedWords', $output, 'scrapm_blacklisted_word_management');    
        
    }
}